#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan  2 14:46:25 2022

@author: liqingsimac
"""

###############################################
##简单过程
import numpy as np
import matplotlib.pyplot as plt

N=80
Y=np.random.randn(N) #独立同分布的随机数
R=np.zeros(N+1)
R[1:N+1]=np.cumsum(Y) #部分和
S=R/np.sqrt(N) 
t=np.linspace(0,1,N+1)

fig=plt.figure()
ax=fig.add_subplot(111)

ax.hlines(y=0,xmin=-0.2,xmax=1.2)
ax.vlines(x=0,ymin=-2,ymax=2)

x1=np.array([0,1/4])
y1=np.array([S[0],S[0]])
x2=np.array([1/4,2/4])
y2=np.array([S[20],S[20]])
x3=np.array([2/4,3/4])
y3=np.array([S[40],S[40]])
x4=np.array([3/4,4/4])
y4=np.array([S[60],S[60]])

ax.plot(t,S,'b-',label=r'Brownian motion $S_t$')
ax.plot(x1,y1,'r--',linewidth=3,label=r'Simple process $C_t$')
ax.plot(x2,y2,'r--',linewidth=3)
ax.plot(x3,y3,'r--',linewidth=3)
ax.plot(x4,y4,'r--',linewidth=3)

ax.set_xlabel('t = time')
ax.set_ylabel(r'$S_t$ and $C_t$')
ax.legend()

fig.savefig('simple-process.png')

###############################################
##简单过程的伊藤随机积分
fig2=plt.figure()
cx=fig2.add_subplot(111)

cx.hlines(y=0,xmin=-0.2,xmax=1.2)
cx.vlines(x=0,ymin=-2,ymax=2)

z1=S[0]
z2=S[20]
z3=S[40]
z4=S[60]

d1b=S[20]-S[0]
d2b=S[40]-S[20]
d3b=S[60]-S[40]
d4b=S[80]-S[60]

cx1=np.arange(0/4,1/4,1/80)
cy1=z1*S[0:20]
cx2=np.arange(1/4,2/4,1/80)
cy2=z1*d1b + z2*(S[20:40]-S[20])
cx3=np.arange(2/4,3/4,1/80)
cy3=z1*d1b + z2*d2b + z3*(S[40:60]-S[40])
cx4=np.arange(3/4,4/4,1/80)
cy4=z1*d1b + z2*d2b + z3*d3b + z4*(S[60:80]-S[60])

cxx=np.hstack([cx1,cx2,cx3,cx4])
cxy=np.hstack([cy1,cy2,cy3,cy4])

cx.plot(cxx,cxy,'r-',linewidth=2,
        label='ito integral for the simple process C')

cx.set_xlabel('t = time')
cx.set_ylabel(r'$I_t(C)$')
cx.legend()

fig.savefig('simple-process-ito-integral.png')






